package com.explorx.sns.puma.im.core.controller;

import com.explorex.puma.common.login.LoginUser;
import com.explorex.puma.common.login.UserContextHolder;
import com.explorex.puma.common.web.HttpResult;
import com.explorx.sns.puma.im.core.chat.ConversationService;
import com.explorx.sns.puma.im.core.chat.UserInfoDomainService;
import com.explorx.sns.puma.im.core.chat.model.ConversationModel;
import com.explorx.sns.puma.im.core.chat.model.MessageModel;
import com.explorx.sns.puma.im.core.chat.model.TopRankModel;
import com.explorx.sns.puma.im.core.chat.model.UserInfoModel;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
import java.util.Optional;

@RestController
@RequestMapping("/api/chat")
public class ConversationController {

    @Resource
    private UserInfoDomainService userInfoDomainService;
    @Resource
    private ConversationService conversationService;

    @GetMapping("/v1/convlist")
    public HttpResult conversationList(@RequestParam("time") long time) {
        final LoginUser userInfo = UserContextHolder.getUserInfo();
        if (userInfo != null) {
            final List<ConversationModel> userConversation = conversationService.getUserConversation(Long.valueOf(userInfo.getUser().getUid()), new Date(time), 0, 100);
            return HttpResult.success(userConversation);
        }
        return HttpResult.success();
    }

    @GetMapping("/v1/conv/message")
    public HttpResult conversationMessage(@RequestParam("cid") Long cid) {
        final LoginUser userInfo = UserContextHolder.getUserInfo();
        if(userInfo!=null){
            final List<MessageModel> conversationMessage = conversationService.getConversationMessage(userInfo.getUser().getUid(),cid);
            return HttpResult.success(conversationMessage);
        }
        return HttpResult.success();
    }

    @GetMapping("/v1/conv/user/message")
    public HttpResult queryUserConversionDetail(@RequestParam("fromUserId") Long fromUserId, @RequestParam("toUserId") Long toUserId) {
        final ConversationModel userConvByPair = conversationService.getUserConvByPair(fromUserId, toUserId);
        final UserInfoModel userInfo = userInfoDomainService.getUserInfo(toUserId);
        userConvByPair.setConvName(Optional.ofNullable(userInfo.getNickName()).orElse(userInfo.getUserName()));
        return HttpResult.success(userConvByPair);
    }


    @PostMapping("/v1/conv/delete")
    public HttpResult deleteConv(@RequestBody Long cid) {
        final LoginUser userInfo = UserContextHolder.getUserInfo();
        final boolean b = conversationService.deleteConv(userInfo.getUser().getUid(), cid);
        return HttpResult.success(b);
    }

    @PostMapping("/v1/conv/top")
    public HttpResult topRankConv(@RequestBody TopRankModel topRank) {
        final LoginUser userInfo = UserContextHolder.getUserInfo();
        final int  topRankId = conversationService.topRank(userInfo.getUser().getUid(), topRank.getCid(),topRank.isTop());
        return HttpResult.success(topRankId);
    }
}
